project(phrases LANGUAGES CXX)

# 组件部分

# hello 动态链接库
add_library(hello SHARED)
target_compile_definitions(hello PRIVATE FUNC_EXPORT) #添加预定义宏FUNC_EXPORT
target_sources(hello PRIVATE hello/hello.cpp)
target_include_directories(
    hello PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/hello/include/>
                 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

# hey 默认静态链接库
add_library(hey)
target_sources(hey PRIVATE hey/hey.cpp)
target_include_directories(
    hey PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/hey/include/>
               $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

# goodbye 默认静态链接库
add_library(goodbye)
target_sources(goodbye PRIVATE goodbye/goodbye.cpp)
set(GOODBYE_HEADERS ${CMAKE_CURRENT_LIST_DIR}/goodbye/include/goodbye.h)
set_target_properties(goodbye PROPERTIES PUBLIC_HEADER ${GOODBYE_HEADERS}) #安装头文件
target_include_directories(
    goodbye
    PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/goodbye/include/>
           $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

# 外部库导入目标
#add_library(libExt IMPORTED)

################################################################################
# 引入GNUInstallDirs，包含了许多CMAKE_INSTALL_*****的变量

include(GNUInstallDirs) 
#安装规则
#安装一：
install(
    TARGETS hello hey
    EXPORT greetings-config
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})

install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/hello/include/
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/greetings/hello)

install(DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/hey/include/
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/greetings/hey)

# 导出上述相关目标（hello和hey）的greeting -config文件
install(
    EXPORT greetings-config
    NAMESPACE ${PROJECT_NAME}::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})

#安装二
install(
    TARGETS goodbye
    EXPORT farewells-config
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/farewells/goodbye)

# 安装头文件
# install(
#     DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/goodbye/include/
#     DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/farewells/goodbye)


# 导出上述相关目标（goodbye）的farewells-config文件
install(
    EXPORT farewells-config
    NAMESPACE ${PROJECT_NAME}::
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})

################################################################################
# 项目部分

configure_file(${PROJECT_NAME}-config.cmake.in ${PROJECT_NAME}-config.cmake
               @ONLY)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})

################################################################################
